﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;

namespace template_dll
{
    public partial class Form6 : Form
    {
        private string GetServ(string filename)
        {
            string str = "";
            using (StreamReader streamReader = new StreamReader(filename))
            {
                while (!streamReader.EndOfStream)
                {
                    str += streamReader.ReadLine();
                }
            }
            return str;
        }

        private DataTable dt_load = new DataTable();
        public Form6()
        {
            InitializeComponent();
        }

        private void Form6_Load(object sender, EventArgs e)
        {
            //- Загрузка выручки 36,6
            dataGridView1.DataSource = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //- Файл загрузки данных
            ExpToExcel ex = new ExpToExcel();
            ex.ImportExcel(dt_load);
            dataGridView1.DataSource = dt_load.DefaultView;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //- Загрузка выручки в базу
            SqlConnection conn_rev = new SqlConnection();
            conn_rev = new SqlConnection(@"Server=" + GetServ("serv.txt") + ";Database=revenue;Integrated Security=True");
            SqlStorProc.sp_params[] sp = new SqlStorProc.sp_params[4];
            foreach (DataRow dr in dt_load.Rows)
            {
                sp[0].name = "@pharmcode";
                sp[0].value = Convert.ToInt32(dr[0].ToString());
                sp[1].name = "@date";
                sp[1].value = Convert.ToDateTime(dr[1].ToString());
                sp[2].name = "@sales";
                sp[2].value = Convert.ToDouble(dr[2].ToString());
                sp[3].name = "@checks";
                sp[3].value = Convert.ToInt32(dr[3].ToString());

                new SqlStorProc().SqlProcExecNonQuery(conn_rev, "pas_366Ins", 300, sp);
            }
            MessageBox.Show("Выручка 36,6 загружена");
        }
    }
}
